package com.hilihili.controller.homeComponent;

import com.hilihili.domain.dto.AudienceDTO;
import com.hilihili.domain.dto.PageDTO;
import com.hilihili.domain.entity.UserCoins;
import com.hilihili.domain.entity.Video;
import com.hilihili.service.ICommentService;
import com.hilihili.service.IUserCoinsService;
import com.hilihili.service.IVideoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;


/**
 * 视频Controller
 *
 * @author xhc
 * @date 2023-11-15
 */
@RestController
@RequestMapping("/hilihili/video")
@CrossOrigin(origins = "http://localhost:8080")
public class VideoController extends BaseController {
    @Autowired
    private IVideoService videoService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IUserCoinsService userCoinsService;

    @PostMapping("/addViewsNumber")
    public void addViewsNumber(@RequestBody AudienceDTO audienceDTO) {
        videoService.addViewsNumber(audienceDTO);
    }

    /**
     * 查询视频列表 按limit  起始下标,查询数量
     */
    @GetMapping("/getVideo")
    public AjaxResult listVideo(PageDTO pageDTO) throws IOException, ClassNotFoundException {
        //设置默认值
        if (pageDTO.getLimit() == 0) {
            pageDTO.setLimit(4);
        }
        return videoService.selectVideoList(pageDTO);
    }


    /**
     * 获取 信息
     * id : 视频id
     */
    @GetMapping(value = "/getDetail/{id}")
    public ResponseEntity getInfo(@PathVariable("id") Long id)// id：视频id
    {
        // TODO: 2024/1/20 查询接口性能优化 jmeter压测测试
        return videoService.selectVideoById(id);
    }


    /**
     * 更新视频的点赞数
     *
     * @param video id：视频id ,isAdd：true:加1 false:减1
     * @return
     */
    @GetMapping(value = "/changeLike")
    public AjaxResult changeVideoLike(Video video) {
        return videoService.updateLikesAndChangeTriplet(video);
    }

    /**
     * 更新视频的点赞数
     *
     * @param video id：视频id ,isAdd：true:加1 false:减1
     * @return
     */
    @GetMapping(value = "/changeCoins")
    public AjaxResult changeVideoCoins(Video video) {
        AjaxResult ajaxResult = videoService.updateCoinsAndChangeTriplet(video);
        if (ajaxResult.isSuccess()){
            // 获取当前用户id
            Long userId = SecurityUtils.getUserId();
            if (userId == null){
                return AjaxResult.error(401,"未登录");
            }
            List<UserCoins> userCoins = userCoinsService.selectUserCoinsListByUserId(userId, video.getId());
            return AjaxResult.success(userCoins);
        }else {
            return AjaxResult.error();
        }
    }


    /**
     * 获取前端 视频详情页面右侧 “接下来播放” 的视频
     *
     * @param videoId 视频id
     * @return {@link AjaxResult}
     */
    @GetMapping("getUserNextVideo")
    public AjaxResult getUserNextVideo(String videoId) {
        return AjaxResult.success(videoService.getLatestVideo(videoId));

    }


}
